﻿using Hr.BLL;
using Hr.IBLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using S22.Imap;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Http;
using LumiSoft.Net.Mail;
using LumiSoft.Net.POP3.Client;

namespace HR.Areas.zhao_kuangtao.Controllers
{
    public class MailController : ApiController
    {
        protected MailService Mls = new MailService();
        int code = 0;
        string msg = "success";
        //
        [HttpGet]
        public JObject Mail1()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.Mail1() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        [HttpPost]
        public JObject DelMail()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            string pk_Mail = data["pk_Mail"];
            msg = "删除成功";
            Mls.DelMail(pk_Mail);
;
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户里的邮件 可以判断在哪个文件夹
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=草稿箱</param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMailInfo(string Pk_User, int ParentID,int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.GetMailInfo(Pk_User,ParentID,page,limit) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 发送邮件表单数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject GetDraftboxInfo()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            string pk_Mail = data["pk_Mail"];
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.GetDraftboxInfo(pk_Mail) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户里的邮件 可以判断在哪个文件夹
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=草稿箱</param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetOutboxInfo(string MailFrom, int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.GetOutboxInfo(MailFrom,page,limit) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 查询所有收件人为当前登录用户里的邮件 回收站
        /// </summary>
        /// <param name="id">当前登录用户id</param>
        /// <param name="ParentID">文件夹位置0=收件箱1=草稿箱</param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetDelMailInfo1(string Pk_User, int page, int limit)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.GetDelMailInfo1(Pk_User,page,limit) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        [HttpPost]
        public JObject UbMail()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            string pk_Mail = data["pk_Mail"];
            msg = "删除成功";
            Mls.UbMail(pk_Mail);

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        [HttpPost]
        public JObject UbDelMail()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            string pk_Mail = data["pk_Mail"];
            msg = "恢复成功";
            Mls.UbDelMail(pk_Mail);

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        
        /// <summary>
        /// 查询一个月之内的未读邮件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
       public object Mail()
        {
            using (POP3_Client c = new POP3_Client())
            {
                c.Connect("pop.163.com", 995, true);
                c.Login("yuetchn@163.com", "OJQTGMRTPGUFWRSG");
                List<object> MailList = new List<object>();
                if (c.Messages.Count > 0)
                {

                    for (var i = c.Messages.Count - 1; i >= 0; i--)
                    {
                        var t = Mail_Message.ParseFromByte(c.Messages[i].MessageToByte());
                        Dictionary<string, object> dt = new Dictionary<string, object>()
                         {
                        {"code",code},
                        {"msg",msg },
                         {"Address", t.From[0].Address},//发件人邮箱地址
                         {"DisplayName", t.From[0].DisplayName},//发件人昵称
                         {"Date", t.Date },//发件时间
                         {"Subject", t.Subject },//邮件标题
                         {"BodyHtmlText", t.BodyHtmlText }//邮件内容
                        };
                        MailList.Add(dt);
                    }
                }
                Dictionary<string, object> result = new Dictionary<string, object>()
                {
                        {"code",code},
                        {"msg",msg },
                    {"data",MailList }
                 };
                return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(result));
            }
           
            
        }
       /// <summary>
       /// 添加内部邮件到草稿箱
       /// </summary>
       /// <returns></returns>
        [HttpPost]
        public object AddDraftBox()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Mail_List r = JsonHelper.DeserializeJsonToObject<Mail_List>(data["Mail"]);
            string EmailContent = data["EmailContent"];
            string CheckDt = "";
            if (data["User"] != null)
            {
                CheckDt = string.Join("|", data["User"].Split(','));
            }

            msg = "保存成功";

            if (!Mls.AddDraftBox(r, EmailContent,CheckDt))
            {
                code = 1;
                msg = "保存失败";
            }
        
        Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject) JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
    }

        [HttpPost]
        public object SendMail1()
        {
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Mail_List r = JsonHelper.DeserializeJsonToObject<Mail_List>(data["Mail"]);
            string EmailContent = data["EmailContent"];
            string CheckDt = "";
            if (data["User"] != null)
            {
                CheckDt = string.Join("|", data["User"].Split(','));
            }

            msg = "发送成功";

            if (!Mls.SendMail1(r, EmailContent, CheckDt))
            {
                code = 1;
                msg = "发送失败";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 发送客户询问邮件并且添加一封客户询问邮件到收件箱
        /// </summary>
        /// <returns></returns>
        [System.Web.Mvc.ValidateInput(false)]
        [HttpPost]
        public object SendMail()
        {
           
            System.Collections.Specialized.NameValueCollection data = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象

            //实例化两个必要的
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            string MailFrom = "yuetchn@163.com";
            string Addressee = data["Addressee"];
            string MailboxTitle = data["MailboxTitle"];
            string EmailContent = data["EmailContent"];


            //发送邮箱地址
            mail.From = new MailAddress("yuetchn@163.com");

            //收件人(可以群发)
            //mail.To.Add(new MailAddress("yuetchn@163.com"));
            mail.To.Add(new MailAddress(Addressee));

            //是否以HTML格式发送
            mail.IsBodyHtml = true;
            //主题的编码格式
            mail.SubjectEncoding = Encoding.UTF8;
            //邮件的标题
            mail.Subject = MailboxTitle.ToString();
            //内容的编码格式
            mail.BodyEncoding = Encoding.UTF8;
            //邮件的优先级
            mail.Priority = MailPriority.Normal;
            //发送内容,带一个图片标签,用于对方打开之后,回发你填写的地址信息
            mail.Body = @EmailContent.ToString();
            //收件人可以在邮件里面
            mail.Headers.Add("Disposition-Notification-To", "回执信息");

            //发件邮箱的服务器地址
            smtp.Host = "smtp.163.com";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Timeout = 10000;
            //是否为SSL加密
            smtp.EnableSsl = true;
            //设置端口,如果不设置的话,默认端口为25
            smtp.Port = 25;
            smtp.UseDefaultCredentials = false;
            //验证发件人的凭据
            smtp.Credentials = new System.Net.NetworkCredential("yuetchn@163.com", "OJQTGMRTPGUFWRSG");

            try
            {
                //发送邮件
                smtp.Send(mail);
                Mls.AddSendMail1(MailFrom, Addressee, MailboxTitle, EmailContent);
                msg = "发送成功";
                //MessageBox.Show("发送成功");
            }
            catch (Exception ex)
            {
                //MessageBox.Show("发送失败：" + e1);
                msg = "发送失败，可能是您的收件人地址错误，或网络波动过大所导致的";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        [HttpGet]
        public JObject MailListInfo ()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code },
                {"msg",msg },
                {"data",Mls.MailListInfo() }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
    }

